var Boug = {};
(function(ns){

   ns.VERSION = '1.0';

   var nativeForEach = Array.prototype.forEach,
       breaker = {},
       OBJ_ID = 0;
    
   var _ = {
      //获取元素
      $ : function( id ) { return typeof id == 'string' ? document.getElementById( id ) : null },

      //是不是数字
      isNumber : function(obj) {
        return !!(obj === 0 || (obj && obj.toExponential && obj.toFixed));
      },

      //遍历
      each : function( obj, iterator, context) {
        if (obj == null) return;
        if ( nativeForEach && obj.forEach === nativeForEach ) {
          obj.forEach(iterator, context);
        } else if (_.isNumber(obj.length)) {
          for (var i = 0, l = obj.length; i < l; i++) {
            if (iterator.call(context, obj[i], i, obj) === breaker) return;
          }
        } else {
          for (var key in obj) {
            if (hasOwnProperty.call(obj, key)) {
              if (iterator.call(context, obj[key], key, obj) === breaker) return;
            }
          }
        }
      },

      //扩展元素
      extend : function( obj ) {
            each(slice.call(arguments, 1), function(source) {
                    for (var prop in source) obj[prop] = source[prop];
            });
            return obj;
      },

      //创建元素
      $el : function( tag, proObj ){
         var el =  document.createElement( tag );

         proObj && _.each(proObj, function(v,i){
             el.setAttribute(i, v);
         });

         return el;
      },

      //获取一个唯一的id
      gid : function(){
          return OBJ_ID++;
      }

   }


   var consoleCache = [];

   ns.buildEl = function( initParams ){
      var el = this.el =  _.$el('div', {
                    id : 'debug_obj_' + _.gid() ,
                    style: 'position:absolute; top:1px;height:400px; width:500px;background:#000; color:#6c6; overflow:scroll'
               }),

          bodyEl = document.getElementsByTagName('body')[0];

      bodyEl.insertBefore( el, bodyEl.childNodes[0]); 

      return el;
   };

   ns.addToConSole = function( str, tag, level ) {

       consoleCache.push({
           msg : str,
           tag : tag,
           level : level
       }); 

       this.el.innerHTML += ( ns.gentor[ tag ] == undefined ? ns.gentor[ 'log' ]() : ns.gentor[ tag ]() ); 
   };

   ns.gentor = {
      log : function(){
          var data = consoleCache[ consoleCache.length - 1 ];
          return  ['<div><span class="log">{#tag} </span>',
                  '<span class="log_level">{#level} </span>',
                  '<span class="log_msg">{#msg} </span></div>'].join('').replace(/{#([^}]+)}/g, function(t,v){  return data[v] })
      }
   }

   ns.log = function( str, level ){
       ns.addToConSole( str, 'log', level || 0 ) 
   } 

})( Boug )


Boug.buildEl();
Boug.log( 12313123 );
